<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String contextPath = request.getContextPath();
    String groupId = request.getParameter("tabId_");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>权限管理</title>
<jsp:include page="/inc/inc.jsp" />
<script type="text/javascript">
$(function(){
	loadMtree();
	//loadCtree();
});
var roleId = "";
var action = 'rolemenuAction';
var loadMtree = function(){
	  var setting = {	            
	            data: {
	                    simpleData: {
	                            enable: true,
	                            idKey: "id",
	                            pIdKey: "pId",
	                            rootPId: 0
	                    }
	            },
	            async: { 
	                    type: "post",
	                    enable: true,
	                    url: 'userAction.do?method=getRightTrees&groupId=<%=groupId%>',
	                    otherParam: {}
	            },
	            callback : {
	                onClick: function(event,treeId,node){
	                	roleId=node.id;
	    				loadCtree();
	                }
	            },

	    };
	    $.fn.zTree.init($("#mtree"), setting);//初始化树对象a
};
var loadCtree = function(){
		var setting = {
	            check: {
	                    enable: true,
	                    chkStyle: "checkbox",
	                    chkboxType: { "Y" : "ps", "N" : "ps" }
	            },
	            data: {
	                    simpleData: {
	                            enable: true,
	                            idKey: "id",
	                            pIdKey: "pId",
	                            rootPId: 0
	                    }
	            },
	            async: { 
	                    type: "post",
	                    enable: true,
	                    url: action+'.do?method=list&groupId=<%=groupId%>',
	                    otherParam: {"roleId":roleId,"groupId":<%=groupId%>}
	            },
	    };
	    $.fn.zTree.init($("#ctree"), setting);//初始化树对象a
};

function cheackAll(){
	var treeObj = $.fn.zTree.getZTreeObj("ctree");
	treeObj.checkAllNodes(true);
}

function unCheackAll(){
	var treeObj = $.fn.zTree.getZTreeObj("ctree");
	treeObj.checkAllNodes(false);
}

function expandAll(){	
	var treeObj = $.fn.zTree.getZTreeObj("ctree");
	treeObj.expandAll(true);	
}

function unexpandAll(){	
	var treeObj = $.fn.zTree.getZTreeObj("ctree");
	treeObj.expandAll(false);	
}

function getChecked(){
	var treeObj = $.fn.zTree.getZTreeObj("ctree");
	var nodes = treeObj.getCheckedNodes(true);
	var s = '';
	for(var i=0; i<nodes.length; i++){
		if (s != '') s += ',';
		s += nodes[i].id;
	}
	return s;
}

function save(){
	if(roleId==""){
		alertError("请选择对应角色!");
		return;
	}
	$.ajax({
		url:'<%=contextPath%>/'+action+'.do?method=save',
		type: 'post',
	    data:{
	    	ids:getChecked(),
	    	roleId:roleId
	    },
	    dataType: 'json',
	    error: function(){
	        alertErr('授权失败!');
	    },
	    success: function(data){
	    	alertInfo('授权成功!');
	    }
	});
}
</script>
</head>
<body class="easyui-layout">
	<div region="west" border="true" title="角色" class="cs-west" style="width:180px;height: 100%" border="false">
		<ul id="mtree"  class='ztree'></ul>
	</div>
	<div  region="center" border="true" title="菜单权限" border="false" style="height:100%">
		<div toolbar="#dlg-toolbar"></div>
		<div id="dlg-toolbar">
			 <a class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="cheackAll()">全选</a>
			 <a class="easyui-linkbutton" iconCls="icon-no" plain="true" onclick="unCheackAll()">清空</a>
			 <a class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="save()">授权</a>
			 <a class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="expandAll()">展开</a>
			 <a class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="unexpandAll()">收缩</a>
		</div>
		<ul id="ctree" class="ztree"></ul>
	</div>
</body>
</html>